﻿using Castle.Windsor;
using Castle.MicroKernel.Lifestyle;
using Shape.Mvp.Commands;
using System.Diagnostics.Contracts;

namespace Shape.Mvp.Ioc
{
   public class WindsorCommandFactory : ICommandFactory
   {
      private IWindsorContainer _container;

      public WindsorCommandFactory(IWindsorContainer container)
      {
         Contract.Requires(container != null);

         _container = container;
      }

      public ICommand CreateCommand<TCommand>() where TCommand : ICommand
      {
         using (_container.BeginScope())
         {
            return _container.Resolve<TCommand>();
         }
      }

      public ICommand<TArg> CreateCommand<TCommand, TArg>() where TCommand : ICommand<TArg>
      {
         using (_container.BeginScope())
         {
            return _container.Resolve<TCommand>();
         }
         
      }
   }
}
